package com.biz.primus.ms.depot.dao.specfication;

import com.biz.primus.model.depot.vo.DepotDeliverPriceListReqVo;
import com.biz.primus.ms.depot.dao.po.DepotDeliverPrice;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author JKLiues
 * @date 2017/10/23
 */
@AllArgsConstructor
public class DepotDeliverPriceSpecification implements Specification<DepotDeliverPrice> {

    private DepotDeliverPriceListReqVo reqVo;


    @Override
    public Predicate toPredicate(Root<DepotDeliverPrice> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
        List<Predicate> predicates = new ArrayList<>();
        if (StringUtils.isNotBlank(reqVo.getDepotCode())) {
            predicates.add(cb.equal(root.get("depotCode").as(String.class), reqVo.getDepotCode()));
        }
        return cb.and(predicates.toArray(new Predicate[predicates.size()]));
    }
}
